import React, { Component } from 'react';

export default class App extends Component {
  state = {
    isShow: true,
  };

  render() {
    const { isShow } = this.state;
    return (
      <div>
        <button onClick={() => this.setState({ isShow: !isShow })}>点我</button>
        {isShow && <Child></Child>}
      </div>
    );
  }
}

class Child extends React.Component {
  resizeFn = () => {
    console.log(123);
  };

  // 类似: vue中的beforeDestroy
  // 场景: 1. 清除定时器  2.取消监听的事件 3. 做清除的动作
  componentWillUnmount() {
    console.log(' componentWillUnmount ----->  ');
    window.removeEventListener('resize', this.resizeFn);
  }

  componentDidMount() {
    window.addEventListener('resize', this.resizeFn);
  }

  render() {
    return <div>Child</div>;
  }
}
